const sun_mapDamPop = {};

sun_mapDamPop.Character_initialize = Sprite_Character.prototype.initialize;
Sprite_Character.prototype.initialize = function (character) {
    sun_mapDamPop.Character_initialize.call(this, character);
    if (character.constructor.name === "Game_Player") {
        this._battler = $gameParty.leader();
    }
}

sun_mapDamPop.Character_update = Sprite_Character.prototype.update;
Sprite_Character.prototype.update = function () {
    sun_mapDamPop.Character_update.call(this);
    if (this._battler) {
        this.updateDamagePopup();
    }
}

/* 更新伤害弹出层 */
Sprite_Character.prototype.updateDamagePopup = function () {
    this.setupDamagePopup();
    if (!this._damages) this._damages = [];
    if (this._damages.length > 0) {
        for (const damage of this._damages) {
            damage.update();
        }
        if (!this._damages[0].isPlaying()) {
            this.destroyDamageSprite(this._damages[0]);
        }
    }
};

/* 设置伤害弹出层 */
Sprite_Character.prototype.setupDamagePopup = function () {
    if (this._battler.isDamagePopupRequested()) {
        if (this._battler.isSpriteVisible()) {
            this.createDamageSprite();
        }
        this._battler.clearDamagePopup();
        this._battler.clearResult();
    }
};

/* 创建伤害弹出层 */
Sprite_Character.prototype.createDamageSprite = function () {
    const last = this._damages[this._damages.length - 1];
    const sprite = new Sprite_Damage();
    if (last) {
        sprite.x = last.x + 8;
        sprite.y = last.y - 16;
    } else {
        sprite.x = this.x + this.damageOffsetX();
        sprite.y = this.y + this.damageOffsetY();
    }
    sprite.setup(this._battler);
    this._damages.push(sprite);
    this.parent.addChild(sprite);
};
/* 销毁伤害弹出层 */
Sprite_Character.prototype.destroyDamageSprite = function (sprite) {
    this.parent.removeChild(sprite);
    this._damages.remove(sprite);
    sprite.destroy();
};

/* 伤害的偏移坐标 X */
Sprite_Character.prototype.damageOffsetX = function () {
    return 0;
};

/* 伤害的偏移坐标 Y */
Sprite_Character.prototype.damageOffsetY = function () {
    return 0;
};

sun_mapDamPop.gainHp = Game_Battler.prototype.gainHp;
Game_Battler.prototype.gainHp = function (value) {
    sun_mapDamPop.gainHp.call(this, value);
    if (!$gameParty.inBattle()) {
        this.startDamagePopup();
    }
}

class Game_MapAction {

}

Scene_Battle.prototype.update = function () {
    const active = this.isActive();
    $gameTimer.update(active);
    $gameScreen.update();
    this.updateVisibility();
    if (active && !this.isBusy()) {
        this.updateBattleProcess();
    }
    Scene_Message.prototype.update.call(this);
}

Scene_Map.prototype.update = function () {
    const active = this.isActive();
    if (active && !this.isBusy()) {
        this.updateMapBattle();
    }
    Scene_Message.prototype.update.call(this);
    this.updateDestination();
    this.updateMenuButton();
    this.updateMapNameWindow();
    this.updateMainMultiply();
    if (this.isSceneChangeOk()) {
        this.updateScene();
    } else if (SceneManager.isNextScene(Scene_Battle)) {
        this.updateEncounterEffect();
    }
    this.updateWaitCount();
}

Scene_Map.prototype.updateMapBattle = function () {
    MapBattleManager.update();
}